/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.auxgroup.smarthome;

import com.auxgroup.smarthome.entity.UserDeviceDemoEntity;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@SpringBootApplication
public class SpringBootMain {
    
    public static void main(final String[] args) {
        SpringApplication.run(SpringBootMain.class, args);
    }

    public static final String PREFIX_JOB = "job_test_demo";

    public static final String JOB_TEST_PATTERN = "job_test_demo*";

    @RequestMapping(value = "index")
    public String index(){
        return "job_test_demo 相关数据当前长度 ：" + count.get();
    }

    @Autowired
    private ObjectRedis objectRedis;

    @PostConstruct
    public void init(){
        Set<String> allKeys = objectRedis.getAllKeys(JOB_TEST_PATTERN);
        if (allKeys.size() < 100000){
            final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
            for (int i = 0 ; i < 100 ; i++) {
                executorService.execute(new JobTak(objectRedis));
            }
            executorService.execute(new ShutdownTask(executorService));
        }
        count.set(allKeys.size());
    }

    private static AtomicInteger count = new AtomicInteger();

    /**
     * 增加10万条随机数据到缓冲里面 用于测试使用
     */
    static class JobTak implements Runnable{

        private final ObjectRedis objectRedis;

        public JobTak(final ObjectRedis objectRedis) {
            this.objectRedis = objectRedis;
        }
        @Override
        public void run() {
            for (int j = 0 ; j < 1000 ; j++) {
                UserDeviceDemoEntity info = new UserDeviceDemoEntity();
                objectRedis.add(PREFIX_JOB + info.getDid(),1000l,info);
                Set<String> allKeys = objectRedis.getAllKeys(JOB_TEST_PATTERN);
                count.set(allKeys.size());
            }
        }
    }

    /**
     * 任务完成后 销毁所有线程
     */
    static class ShutdownTask implements Runnable{

        private final ExecutorService executorService;

        public ShutdownTask(ExecutorService executorService) {
            this.executorService = executorService;
        }
        @Override
        public void run() {
            while (true){
                if (count.get() >= 100000){
                    if(!executorService.isShutdown()){
                        executorService.shutdown();
                    }
                }
            }
        }
    }





}
